//
// Author    : Pablo Bosch 
// Contact   : pboschvivancos@epo.org
// Created   : 17/03/2008
//

var XML_STD = {
  CLAUSESFILE:"data/stdcls.xml",
  DEFCLAUSESFILE:"defs/stdcls.xml",
  STDCLSDOM:new ActiveXObject("Msxml2.DOMDocument"),
  DEFSTDCLSDOM:new ActiveXObject("Msxml2.DOMDocument"),
  PREFDEFAULTLANG:"en",
  PREFDEFAULTLAW:"epc"
}

if(!XML_STD.STDCLSDOM  || !XML_STD.DEFSTDCLSDOM) {
  alert("Error creating object XMLCLAUSES");
}

function LoadStdClauses() {
  dos_loadxmlinit(XML_STD.STDCLSDOM,XML_STD.DEFCLAUSESFILE,XML_STD.CLAUSESFILE);
  XML_STD.DEFSTDCLSDOM.load(XML_STD.DEFCLAUSESFILE);
}

function SaveStdClauses() {
  XML_STD.STDCLSDOM.save(XML_STD.CLAUSESFILE);
}

function ReloadStdClauses() {
  XML_STD.STDCLSDOM.load(XML_STD.CLAUSESFILE);
}


XML_STD.sortstdclausebytype=function(a,b) {
  return actionsortalpha("type",a,b); 
}


XML_STD._StdClause_Iterator=function(dom,attr,val) {
  this.dom=dom;
  this._idx=0;
  this._nodes;
  this._query="";
  //alert("Objection "+(attr||"")+":"+(val||""));
  if(attr && val) {
    this._query="[@"+attr+"=\""+val+"\"]";
    this._nodes=this.dom.selectNodes("/objections/objection[ @"+attr+"=\""+val+"\"]");
  } else if(attr) {
    this._query="["+attr+"]";    
    this._nodes=this.dom.selectNodes("/objections/objection[ "+attr+" ]");
  } else {
     this._nodes=new Array();
    var tmp=this.dom.selectSingleNode("/objections").childNodes;
    for(var n=0; n< tmp.length; n++) {
      this._nodes.push(tmp[n]);
    }
    this._nodes.sort(XML_STD.sortstdclausebytype);
  }
}
XML_STD._StdClause_Iterator.prototype.getUniLaw=function() {
  return this._nodes[this._idx].getAttribute("unilaw")|| XML_STD.PREFDEFAULTLAW;
}
XML_STD._StdClause_Iterator.prototype.setUniLaw=function(law) {
  this._nodes[this._idx].setAttribute("unilaw",law);
}

XML_STD._StdClause_Iterator.prototype.getUniLang=function() {
  return this._nodes[this._idx].getAttribute("unilang")|| XML_STD.PREFDEFAULTLANG;
}

XML_STD._StdClause_Iterator.prototype.length = function() { return this._nodes.length; }
XML_STD._StdClause_Iterator.prototype.end = function () { return this._idx>= this._nodes.length; }
XML_STD._StdClause_Iterator.prototype.next = function() { this._idx++;}
// to use outside comms
  
XML_STD._StdClause_Iterator.prototype._getText  = function(lang,law) {
  var nlaw=this._nodes[this._idx].selectSingleNode(law.toLowerCase());
  if(nlaw) {
    var nlang =nlaw.selectSingleNode(lang.toLowerCase());
    if(nlang) return nlang.text;
  }
  return "";
}


  
XML_STD._StdClause_Iterator.prototype.getText  = function(lang,law,dbg) { 
  var _law = law ?  law.toLowerCase() : XML_STD.PREFDEFAULTLAW;       
  var _lang= lang ? lang.toLowerCase() : XML_STD.PREFDEFAULTLANG;
  if(this.end()) {      
    return "??{StdClause "+(attr||"")+":"+(val||"")+ " not found or not active}??";
  }
  if(dbg)  alert(this._query+" :getText "+_lang+" "+_law+ " ("+lang+" "+law+")");

  var lawelem=this._nodes[this._idx].selectSingleNode(_law);
  if(lawelem) { // law found 
    var langelem=lawelem.selectSingleNode(_lang);
    // try lang and law
    if(langelem && langelem.text !="") {
      if(dbg) alert("Found "+_law+" "+_lang+" => "+langelem.text);
      return langelem.text;
    }
    // try english instead of lang
    if(_lang  != XML_STD.PREFDEFAULTLANG) {
      var langelem=lawelem.selectSingleNode(XML_STD.PREFDEFAULTLANG);
      // try lang and law
      if(langelem && langelem.text !="") {
	if(dbg) alert("Found "+_law+" "+XML_STD.PREFDEFAULTLANG+" => "+langelem.text);
	return langelem.text;
      }
    }
  }
  // law not found, try default (epc)
  if(_law  != XML_STD.PREFDEFAULTLAW) {  
    lawelem=this._nodes[this._idx].selectSingleNode(XML_STD.PREFDEFAULTLAW);
    if(lawelem) {
      var langelem=lawelem.selectSingleNode(_lang);
      // try lang and default law
      if(dbg) alert("Try "+XML_STD.PREFDEFAULTLAW+" "+_lang);
      if(langelem && langelem.text !="") {
	if(dbg) alert("Found "+XML_STD.PREFDEFAULTLAW+" "+_lang+" => "+langelem.text);
	return langelem.text;
      }
      //alert("Not found "+XML_STD.PREFDEFAULTLAW+" "+_lang);      
      // try default lang and default law
      if(_lang  != XML_STD.PREFDEFAULTLANG) {
	if(dbg) alert("Try "+XML_STD.PREFDEFAULTLAW+" "+"en");  
	return this.getText(XML_STD.PREFDEFAULTLANG,XML_STD.PREFDEFAULTLAW);
      }
    } 
  }
  return "{ NOT FOUND "+this._query +"}";
}

XML_STD._StdClause_Iterator.prototype.setText  = function(txt,lang,law) { 
  var _law   =(law && law.toLowerCase()) || XML_STD.PREFDEFAULTLAW;
  var _lang  =(lang&& lang.toLowerCase()) || XML_STD.PREFDEFAULTLANG;
  
  var lawelem=this._nodes[this._idx].selectSingleNode(_law);
  if(!lawelem) {
    //alert("Create "+_law);
    lawelem=this.dom.createElement(_law);
    this._nodes[this._idx].appendChild(lawelem);
  }
  var langelem=lawelem.selectSingleNode(_lang);
  if(!langelem) {
    //alert("Create "+_lang);
    langelem=this.dom.createElement(_lang);
    lawelem.appendChild(langelem);
  }
  langelem.text=txt;
}

// for use in select boxes 
XML_STD._StdClause_Iterator.prototype.getValue = function() {
  return this._nodes[this._idx].getAttribute("label");
}
XML_STD._StdClause_Iterator.prototype.set_attr= function(name,val) {
  this._nodes[this._idx].setAttribute(name,val);
}
XML_STD._StdClause_Iterator.prototype.get_attr= function(name) {
    return this._nodes[this._idx].getAttribute(name) || "";
}
  
XML_STD._StdClause_Iterator.prototype.label = function() { return this.get_attr("label");}
XML_STD._StdClause_Iterator.prototype.desc = function() { return this.get_attr("desc");}
XML_STD._StdClause_Iterator.prototype.type = function() { return this.get_attr("type");}
      
XML_STD._StdClause_Iterator.prototype.copy= function(label) {
  var newnode=this._nodes[this._idx].cloneNode(true); // backup of old node
  this.dom.selectSingleNode("objections").appendChild(newnode);
  if(label) {
    this._nodes[this._idx].setAttribute("label",label); // work on original as new node
  }
}
XML_STD._StdClause_Iterator.prototype.add = function(label) {
  var newnode=this.dom.createElement("objection");
  newnode.setAttribute("label",label);
  this.dom.selectSingleNode("objections").appendChild(newnode);    
}

XML_STD._StdClause_Iterator.prototype.rm = function() {
  this.dom.selectSingleNode("objections").removeChild(this._nodes[this._idx]);
  this.end = function() { return true;}
}
  

function stdcls_iter_not_found(query) { 
  this.query=query;
}
stdcls_iter_not_found.prototype.end= function() { return true;}
stdcls_iter_not_found.prototype.getText=function(lang,val){
  return "{StdClause "+this.query+" not found or not active}";
}


function CreateStdClause(type,label) {
  var newnode= XML_STD.STDCLSDOM.createElement("objection");
  newnode.setAttribute("type",type);
  newnode.setAttribute("label",label);
  XML_STD.STDCLSDOM.selectSingleNode("objections").appendChild(newnode);
}


function StdClause_Iterator(attr,val) {
  return new XML_STD._StdClause_Iterator(XML_STD.STDCLSDOM,attr,val);
}
function DefStdClause_Iterator(attr,val) {
  return new XML_STD._StdClause_Iterator(XML_STD.DEFSTDCLSDOM,attr,val);
}


function getMenuC() {
  //alert("getMenuC");
  var menuC=new Array("");
  var clauses=new XML_STD._StdClause_Iterator(XML_STD.STDCLSDOM," @showC=\"yes\" && @active=\"yes\"");
  while(!clauses.end()) {
    //alert(clauses.label());
    menuC.push(clauses.label());
    clauses.next();
  }
  return menuC;
}

function getMenuGC() {
  //alert("getMenuC");
  var menuC=new Array();
  var clauses=new XML_STD._StdClause_Iterator(XML_STD.STDCLSDOM," @showGC=\"yes\" && @active=\"yes\"");
  while(!clauses.end()) {
    //alert(clauses.label());
    menuC.push(clauses.label());
    clauses.next();
  }
  return menuC;
}

function getMenuH() {
  var menuH=new Array();
  var clauses=new XML_STD._StdClause_Iterator(XML_STD.STDCLSDOM," @showH=\"yes\" && @active=\"yes\"");
  while(!clauses.end()) {
    menuH.push(clauses.label());
    clauses.next();
  }
  return menuH;
}

LoadStdClauses();
